package com.threeox.commonlibrary.ui.view.impl.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.KeyboardShortcutGroup;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.CommonApplcation;
import com.threeox.commonlibrary.entity.engine.event.BroadCastMessage;
import com.threeox.commonlibrary.entity.engine.event.EventMessage;
import com.threeox.commonlibrary.entity.engine.model.base.BaseModelMsg;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.commonlibrary.ui.view.engineview.base.CommonModelView;
import com.threeox.commonlibrary.ui.view.inter.engine.base.IBaseExtend;
import com.threeox.commonlibrary.ui.view.inter.reload.IReloadView;
import com.threeox.commonlibrary.ui.view.inter.titlebar.ITitleBarView;
import com.threeox.commonlibrary.ui.view.loadview.ILoadDialog;
import com.threeox.commonlibrary.util.BroadCastUtils;
import com.threeox.commonlibrary.util.CommonConstant;
import com.threeox.commonlibrary.util.DialogUtil;
import com.threeox.commonlibrary.util.engine.EventFactory;
import com.threeox.commonlibrary.util.engine.VerifyFactory;
import com.threeox.utillibrary.util.ToastUtils;

import java.util.List;

/**
 *
 * @ClassName: BaseModelExtend
 *
 * @Description: TODO(基本的模型扩展类   封装一些常用的函数)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/7 17:28
 *
 * @version 1.0
 *
 */
public abstract class CommonModelExtend implements IBaseExtend {

    // 传入的文件名称
    protected final String FILENAMEMODEL = CommonConstant.FILENAMEMODEL;
    // 传入的标题名称
    protected final String TITLENAMEMODEL = CommonConstant.TITLENAMEMODEL;

    /**
     * 取消
     */
    protected final int RESULT_CANCELED = Activity.RESULT_CANCELED;
    /**
     * 成功
     */
    protected final int RESULT_OK = Activity.RESULT_OK;
    /**
     * 开始用户定义的活动结果。
     */
    protected final int RESULT_FIRST_USER = Activity.RESULT_FIRST_USER;

    protected final String TAG = this.getClass().getName();

    protected Intent mIntent;
    protected Activity mActivity;
    protected Fragment mFragment;
    protected ILoadDialog mLoadDialog;
    protected IReloadView mReloadView;
    protected ITitleBarView mTitleBarView;
    protected CommonApplcation mApplcation;
    protected CommonModelView mCommonModelView;
    protected BaseModelMsg baseModelessage;

    protected EventFactory mEventFactory;
    protected VerifyFactory mVerifyFactory;
    protected BroadCastUtils mBroadCastUtils;

    @Override
    public void initModelMessage(BaseModelMsg modelMessage, CommonModelView commonModelView) {
        this.baseModelessage = modelMessage;
        initBaseExtend(commonModelView);
    }

    protected void initBaseExtend(CommonModelView commonModelView) {
        this.mCommonModelView = commonModelView;
        this.mIntent = mCommonModelView.getIntent();
        this.mApplcation = CommonApplcation.getInstance();
        this.mLoadDialog = mCommonModelView.getLoadDialog();
        this.mReloadView = mCommonModelView.getReloadView();
        this.mTitleBarView = mCommonModelView.getTitleBarView();
        this.mEventFactory = mCommonModelView.getEventFactory();
        this.mVerifyFactory = mCommonModelView.getVerifyFactory();
        this.mBroadCastUtils = mCommonModelView.getBroadCastUtils();
    }

    /*==================IBaseExtend 的函数=====================*/
    @Override
    public void initActivity(Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public void initFragment(Fragment fragment) {
        this.mFragment = fragment;
    }

    /*=====================校验的回调接口=====================*/
    @Override
    public boolean onBeforeVerify(View view, EventMessage eventMessage, JSONObject value) {
        return true;
    }

    @Override
    public void onAfterVerify(View view, EventMessage eventMessage, JSONObject value, boolean isVerify) {

    }
    /*=====================校验的回调接口=====================*/

    /*=====================事件的回调接口=====================*/
    @Override
    public boolean onBeforeEvent(View view, EventMessage eventMessage, JSONObject requestParams, boolean isVerify, Object... params) {
        return true;
    }

    @Override
    public void onAfterEvent(View view, EventMessage eventMessage, Object... params) {

    }
    /*=====================事件的回调接口=====================*/

    /*=====================事件请求操作的回调接口=====================*/

    @Override
    public boolean onBeforeEventSuccess(BaseRequestMsg requestMsg, String url, int id, String msg, Object data, EventMessage eventMessage) {
        return true;
    }

    @Override
    public void onAfterEventSuccess(BaseRequestMsg requestMsg, String url, int id, String msg, Object data, EventMessage eventMessage) {

    }

    @Override
    public void onEventError(BaseRequestMsg requestMsg, String url, int id, Object code, String msg, EventMessage eventMessage) {

    }

    @Override
    public void inEventProgress(BaseRequestMsg requestMsg, float progress, long total, int id, String url, EventMessage eventMessage) {

    }
    /*=====================事件请求操作的回调接口=====================*/

    /*========================广播请求的函数==========================*/
    @Override
    public boolean onBeforeSendBroadCast(EventMessage eventMessage, BroadCastMessage castMessage, Object data) {
        return true;
    }

    /*========================广播请求的函数==========================*/

    @Override
    public void onReceive(Context context, Intent intent, String action) {

    }

    /*=====================请求中和失败的回调接口=====================*/

    @Override
    public void onRequestError(BaseRequestMsg requestMsg, String url, int id, Object code, String msg) {

    }

    @Override
    public void onRequestInProgress(BaseRequestMsg requestMsg, float progress, long total, int id, String url) {

    }
    /*=====================请求中和失败的回调接口=====================*/

    /*=====================各种状态变化的回调接口=====================*/

    @Override
    public void requestIn(String message) {
    }

    @Override
    public void requestFault(Object data, String msg) {
    }

    @Override
    public void requestSucceed(Object data) {
    }

    @Override
    public void notData(String message) {
    }

    @Override
    public void networkClose(String message) {
    }

    @Override
    public void networkWeak(String message) {
    }

    /*=====================各种状态变化的回调接口=====================*/

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    }
    /*==================IBaseExtend 的函数=====================*/

    /*==============标题栏的扩展函数==================*/

    @Override
    public boolean onLeftLayoutClick(View v) {
        return true;
    }

    @Override
    public void onRightLayoutClick(View v) {
    }

    @Override
    public void onCenterLayoutClick(View v) {
    }
    /*==============标题栏的扩展函数==================*/

    /*====================监听手机屏幕的按键=========================*/
    @Override
    public Boolean onKeyDown(int keyCode, KeyEvent event) {
        return null;
    }

    @Override
    public Boolean onKeyUp(int keyCode, KeyEvent event) {
        return null;
    }

    @Override
    public Boolean onKeyLongPress(int keyCode, KeyEvent event) {
        return null;
    }

    @Override
    public Boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        return null;
    }

    @Override
    public Boolean onKeyShortcut(int keyCode, KeyEvent event) {
        return null;
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {

    }

    @Override
    public Boolean onTouchEvent(MotionEvent event) {
        return null;
    }
    
    /*====================监听手机屏幕的按键=========================*/

    /*==============Activity和Fragment的生命周期==================*/

    @Override
    public void onCreate(Bundle savedInstanceState) {
    }

    @Override
    public boolean onStart() {
        return true;
    }

    @Override
    public boolean onRestart() {
        return true;
    }

    @Override
    public boolean onResume() {
        return true;
    }

    @Override
    public boolean onStop() {
        return true;
    }

    @Override
    public boolean onPause() {
        return true;
    }

    @Override
    public boolean onDestroy() {
        try {
            if (mLoadDialog != null) {
                DialogUtil.dismissDialog(mLoadDialog);
            }
        } catch (Exception e) {
        }
        return true;
    }

    @Override
    public boolean onBackPressed() {
        return true;
    }

    @Override
    public void onAttach(Activity activity) {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    }

    @Override
    public boolean onDestroyView() {
        return true;
    }

    @Override
    public boolean onDetach() {
        return true;
    }

    /*==============Activity和Fragment的生命周期==================*/

    /*===============自定义抽象的公用函数==================*/
    /**
     * 初始化View
     */
    protected void initView() {
    }

    /**
     * 初始化数据
     */
    protected void initData() {
    }

    /**
     * 初始化事件
     */
    protected void setListener() {
    }

    /**
     * 设置标题
     *
     * @param text
     */
    public void setTitle(CharSequence text) {
        if (mTitleBarView != null) {
            mTitleBarView.setTitle(text);
        }
    }

    /**
     * 设置返回值
     * @param resultCode
     */
    protected void setResult(int resultCode) {
        if (mActivity != null) {
            mActivity.setResult(resultCode);
        }
    }

    /**
     * 设置返回值
     *
     * @param resultCode
     * @param data
     */
    protected void setResult(int resultCode, Intent data) {
        if (mActivity != null) {
            mActivity.setResult(resultCode, data);
        }
    }

    /**
     * 退出当前Activity
     */
    protected void finish() {
        if (mActivity != null) {
            mActivity.finish();
        } else if (mFragment != null) {
            mFragment.getActivity().finish();
        }
    }

    /**
     * 根据Id得到View,缓存至内存
     *
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int id) {
        if (null != mCommonModelView) {
            return mCommonModelView.getView(id);
        }
        return null;
    }

    /**
     * 根据Id得到View
     *
     * @param id
     */
    public <T extends View> T findViewById(int id) {
        if (null != mCommonModelView) {
            return (T) mCommonModelView.findViewById(id);
        }
        return null;
    }

    /**
     * 显示Toast
     *
     * @param id
     */
    protected void showToast(int id) {
        Context context = getContext();
        if (context != null) {
            String message = context.getString(id);
            showToast(message);
        }
    }


    /**
     * 显示Toast
     *
     * @param message
     */
    protected void showToast(String message) {
        Context context = getContext();
        if (context!= null) {
            ToastUtils.showShortToastSafe(context, message);
        }
    }

    /**
     * 得到Context
     *
     * @return
     */
    protected Context getContext() {
        Context context = null;
        if (mActivity != null) {
            context = mActivity;
        } else if (mFragment != null) {
            context = mFragment.getContext();
        } else if (mCommonModelView != null) {
            context = mCommonModelView.getContext();
        } else if (mApplcation != null) {
            context = mApplcation;
        }
        return context;
    }

    /**
     * 得到Activity
     *
     * @return
     */
    protected Activity getActivity() {
        Activity activity = null;
        if (mActivity != null) {
            activity = mActivity;
        } else if (mFragment != null) {
            activity = mFragment.getActivity();
        } else if (mCommonModelView != null) {
            activity = (Activity) mCommonModelView.getContext();
        }
        return activity;
    }

    /**
     * 得到字符串
     *
     * @param stringId
     * @return
     */
    protected String getString(int stringId) {
        Context context = getContext();
        if (context != null) {
            return context.getString(stringId);
        } else {
            return null;
        }
    }

    /**
     * 加载动画弹窗
     *
     * @param message
     */
    protected ILoadDialog showLoadDialog(String message) {
        return mCommonModelView.showLoadDialog(message);
    }

    /**
     * 隐藏加载弹窗
     */
    public void dismissDialog() {
        mCommonModelView.dismissDialog();
    }

    public <T extends CommonModelView> T getCommonModelView() {
        return (T) mCommonModelView;
    }

    /*===============自定义抽象的公用函数==================*/

}
